// $Id: bool_ref.hpp 2 2009-09-02 08:47:42Z joseph2002 $
/*
 * Copyright 2009 Joseph Wu
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 *
 * For more information, see http://xpcomplus.sourceforge.net
 */

#ifndef XPCOM_PROXY_BOOL_REF_HPP
#define XPCOM_PROXY_BOOL_REF_HPP

#include <xpcom/config.hpp>
#include <xpcom/types.hpp>

#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_PREFIX
#endif

namespace xpcom {
namespace proxy {

//
// Proxy of bool_t for use as bool.
//
class bool_ref
{
public:
    bool_ref() :
        b_(false_)
    {
    }

    bool_ref(const bool_ref& rhs) :
        b_(rhs.b_)
    {
    }

    bool_ref(bool b) :
        b_(b ? true_ : false_)
    {
    }

    bool_t get() const
    {
        return b_;
    }

    operator bool() const
    {
        return b_ != false_;
    }

    bool operator!() const
    {
        return b_ == false_;
    }

    bool_ref& operator=(const bool_ref& rhs)
    {
        b_ = rhs.b_;
        return *this;
    }

    bool_ref& operator=(bool b)
    {
        b_ = b ? true_ : false_;
        return *this;
    }

    void swap(bool_ref& rhs)
    {
        bool_t tmp = b_;
        b_ = rhs.b_;
        rhs.b_ = tmp;
    }

private:
    bool_t b_;
};

// swap
inline void swap(bool_ref& lhs, bool_ref& rhs)
{
    lhs.swap(rhs);
}

} // namespace xpcom::proxy
} // namespace xpcom

#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif

#endif // XPCOM_PROXY_BOOL_REF_HPP
